package org.aphelie.torg.ruler.internal.ui.editor;

import org.aphelie.torg.ruler.core.IModelChangedEvent;
import org.aphelie.torg.ruler.core.IModelChangedListener;
import org.eclipse.core.resources.IProject;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;

public class TGRSection extends SectionPart implements IModelChangedListener, IContextPart {
	
	private TGRFormPage fPage;
	
	public TGRSection(TGRFormPage page, Composite parent, int style) {
		this(page, parent, style, true);
	}
	
	public TGRSection(TGRFormPage page, Composite parent, int style, boolean titleBar) {
		super(parent, page.getManagedForm().getToolkit(), titleBar ? (ExpandableComposite.TITLE_BAR | style) : style);
		fPage = page;
		initialize(page.getManagedForm());
		getSection().clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
		getSection().setData("part", this); //$NON-NLS-1$		
	}

	public TGRFormPage getPage() {
		return fPage;
	}
	
	protected IProject getProject() {
		return fPage.getTGREditor().getCommonProject();
	}

	@Override
	public void modelChanged(IModelChangedEvent e) {
		if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED)
			markStale();
	}
	
	@Override
	public String getContextId() {
		return null;
	}
	
	@Override
	public void fireSaveNeeded() {
		markDirty();
		if (getContextId() != null)
			getPage().getTGREditor().fireSaveNeeded(getContextId(), false);
	}

	@Override
	public boolean isEditable() {
		// getAggregateModel() can (though never should) return null
		//IBaseModel model = getPage().getPDEEditor().getAggregateModel();
		//return model == null ? false : model.isEditable();
		return true;
	}





	@Override
	public void cancelEdit() {
		// TODO Auto-generated method stub
		
	}

}
